<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class FileUrlMenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 查找Admin或系统菜单
        $adminMenu = DB::table('admin_menu')
            ->where('title', 'Admin')
            ->orWhere('title', 'like', '%系统%')
            ->first();

        if (!$adminMenu) {
            // 如果不存在，创建Admin菜单
            DB::table('admin_menu')->insert([
                'parent_id' => 0,
                'order' => DB::table('admin_menu')->max('order') + 1,
                'title' => 'Admin',
                'icon' => 'fa-cogs',
                'extension' => '',
                'show' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ]);

            $adminMenu = DB::table('admin_menu')->where('title', 'Admin')->first();
        }

        if (!DB::table('admin_menu')->where('title', '资源地址')->exists()) {
            DB::table('admin_menu')->insert([
                'parent_id' => $adminMenu->id,
                'order' => DB::table('admin_menu')->max('order') + 1,
                'title' => '资源地址',
                'uri' => '/file_url',
                'icon' => 'fa-link',
                'extension' => '',
                'show' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ]);
        } else {
            DB::table('admin_menu')->where('title', '资源地址')->update([
                'parent_id' => $adminMenu->id,
            ]);
        }

        $this->command->info('资源地址菜单路由填充完成！');
    }
}
